// 获取数据库内容，渲染到页面
function apply() {
  $.ajax({
    url: '/admin/comments',
    success: res => {
      // console.log(res);
      if (res.status === 0) {
        let str = ''
        res.data.forEach(item => {
          // console.log(item);
          str += `
          <tr>
            <td>${item.id}</td>
            <td>${item.uname}</td>
            <td>${item.content}</td>
            <td>${item.cdate.substring(0, 10)}</td>
            <td>
              <button data-id="${item.id}" type="button" class="layui-btn layui-btn-xs layui-btn-danger">删除</button>
            </td>
          </tr>
        `
        });
        $('tbody').html(str)
      } else if (res.status === 1 || res.message === '查询评论列表失败') {
        $("tbody").empty()
      }
    }
  })
}
apply()

// 点击删除按钮就行评论删除
$('tbody').on('click', 'button', function () {
  let id = $(this).data('id')
  // console.log(id);
  layer.confirm('是否删除评论', function (index) {
    //do something
    $.ajax({
      type: 'DELETE',
      url: `/admin/comments/${id}`,
      success: res => {
        // console.log(res);
        if (res.status === 0) {
          layer.msg(res.message, { time: 1500 });
          apply()
        }
      }
    })
    layer.close(index);
  });

})